package com.cloudspace.jindun.adapter;

import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.constants.Constants;
import com.cloudspace.jindun.model.ChannelMemberStatus;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class CurrentChannelDetailAdapter extends BaseAdapter  {
    public static final String TAG = CurrentChannelDetailAdapter.class.getSimpleName();
    private List<ChannelMemberStatus> mData = new ArrayList<>();
    
    private FragmentActivity activity;
    private boolean isPlay;

    public CurrentChannelDetailAdapter(FragmentActivity activity){
        this.activity = activity;
    }

    public void setDatas(List<ChannelMemberStatus> data){
        if (data != null){
            mData = data;
            notifyDataSetChanged();
        }
    }

    public void setPlayState(boolean play){
        isPlay = play;
        notifyDataSetChanged();
    }

    public List<ChannelMemberStatus> getData(){
        return mData;
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(mData)){
            return 0;
        }

        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(mData)){
            return null;
        }

        if (position < 0 || position >= mData.size()){
            return null;
        }

        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_current_channel, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.phone_image_header);
            holder.name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.speakImg = (ImageView) convertView.findViewById(R.id.speakImg);
            holder.detailLy = convertView.findViewById(R.id.phone_detail_ly);
            holder.speakTimeTv = (TextView) convertView.findViewById(R.id.speakTimeTv);
            holder.statusImg = (ImageView) convertView.findViewById(R.id.channel_status_img);
            holder.remarkTv = (TextView) convertView.findViewById(R.id.remarkTv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ChannelMemberStatus person = mData.get(position);

        holder.name.setText(person.name);
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.icon, person.iconUrl,
                Configs.IMAGE_CORNER_RADIUS);

        if (position == 0){
            holder.speakImg.setVisibility(isPlay ? View.VISIBLE : View.GONE);
            holder.speakTimeTv.setVisibility(isPlay ? View.GONE : View.VISIBLE);

            if (!TextUtils.isEmpty(person.departName)){
                holder.remarkTv.setText(person.departName);
                holder.remarkTv.setTextColor(activity.getResources().getColor(R.color.color_e757a1));
            }
        }else{
            holder.speakTimeTv.setVisibility(View.VISIBLE);
            holder.speakImg.setVisibility(View.GONE);

            if (!TextUtils.isEmpty(person.departName)){
                holder.remarkTv.setText(person.departName);
                holder.remarkTv.setTextColor(activity.getResources().getColor(R.color.color_f88717));
            }
        }

        if (person.status.equals(Constants.CHANNEL_STATUS_ONLINE)){
            //在线
            holder.statusImg.setImageResource(R.drawable.channel_online);
        }else if (person.status.equals(Constants.CHANNEL_STATUS_SILENT)){
            //静默
            holder.statusImg.setImageResource(R.drawable.channel_suspend);
        }else if (person.status.equals(Constants.CHANNEL_STATUS_OFFLINE)){
            //不在线
            holder.statusImg.setImageResource(R.drawable.channel_offline);
        }

        String dayDiff = TimeUtils.getFitTimeSpanByNow(Long.parseLong(person.lastActionTime), 1);
        String lastSpeakTimeDate = TimeUtils.millis2String(Long.parseLong(person.lastActionTime));

        String yearMonthDay = lastSpeakTimeDate.split(" ")[0];
        String hhMMSS = lastSpeakTimeDate.split(" ")[1];
        String hhMM = hhMMSS.substring(0,  hhMMSS.lastIndexOf(":"));

        if (TextUtils.isEmpty(dayDiff)){
            //显示hh:mm
            holder.speakTimeTv.setText(hhMM);
        }else {
            holder.speakTimeTv.setText(yearMonthDay);
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView name;
        TextView speakTimeTv;
        ImageView statusImg;
        ImageView speakImg;
        TextView remarkTv;
        View detailLy;
    }
}